using System;
using System.Windows.Forms;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Threading;


namespace caseDrawer
{
	public class RqrdGenerator : AbstractGenerator
	{
		IList<string> brickIds = new List<string>();
		int effectiveRowCount;
				
		public override void generateTestCase(){
			effectiveRowCount = getEffectiveRowCount();
			
			Writer.Senario 			= this.Senario;
			Writer.TestCaseFolder	= this.TestCaseFolder;			
									
			Senario.Comment = " fail with required fields empty \n";
			Writer.write( base.generateTitle_Ngv(Senario) , generateResults_Ngv());
			Thread.Sleep(1000);
		}		
		
		#region All Blank
		
		IList<string> generateResults_Ngv(){
			List<string> results = new List<string>();
			
			results.Add("[STEP]");
			
			int stepnum = 1;
			foreach(DataGridViewRow row in Rows){
				if(row.IsNewRow)
					break;
				
				string id			= row.Cells["cid"].Value.ToString();
				string eventStr 	= convertToAction( row.Cells["caccessEvents"].Value.ToString() );
				string name			= row.Cells["cdescription"].Value.ToString();
				
				string input="";
				string output="";
				
				results.Add(writeSteps_Ngv(stepnum, id,eventStr,name,input,output));

				stepnum++;
			}

			return results;
		}
		
		string writeSteps_Ngv(int stepnum, string brickid,string eventStr,string name,string input,string output){
			brickIds.Add(brickid);
			
			StringBuilder step = new StringBuilder();
			
			step.Append(stepnum);
			step.Append(": ");

			step.Append("User ");
			step.Append(eventStr);
			
			if(eventStr=="input "){
				step.Append(" { Nothing } ");
				step.Append(" at ");
			}

			step.Append(name);
			
			if(stepnum == this.effectiveRowCount){
				step.Append("		-> At [STEP] ");
				step.Append(stepnum);
				step.Append(": ");
				step.Append( getAllReqFailOutput() );
			}
			
			return step.ToString();
		}
		
		#endregion
		
		#region 
		
		string getAllReqFailOutput()
		{
			StringBuilder sb = new StringBuilder();
			DataTable tb = Database.Instance.loadResults(brickIds, 'N' , "input=''");
			for(int i=0; i < tb.Rows.Count; i++){				
				sb.Append(tb.Rows[i]["output"].ToString());
				sb.Append(", ");
			}
			return sb.ToString();
		}
		
		int getEffectiveRowCount(){
			int cnt =0;
			
			foreach(DataGridViewRow row in Rows){
				if(row.IsNewRow)
					break;
				cnt++;
			}
			
			return cnt;
		}
	
	}

}

